/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * InfoPane.java 2008-11-10
 */
package ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.infopane;

import ie.ucc.cs.anggao.finalproject.client.model.Query;
import ie.ucc.cs.anggao.finalproject.client.support.searchengine.AggregatedSearcher;
import ie.ucc.cs.anggao.finalproject.client.support.searchengine.BaseSearcher;
import ie.ucc.cs.anggao.finalproject.client.support.searchengine.FlickrSearcher;
import ie.ucc.cs.anggao.finalproject.client.ui.BasePane;
import ie.ucc.cs.anggao.finalproject.client.ui.event.QueryBackEvent;
import ie.ucc.cs.anggao.finalproject.client.ui.event.QueryEvent;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeImages;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A {@link BasePane} showing the reference information
 * 
 * @author GaoAng
 * 
 */
public class InfoPane extends BasePane {

	private DecoratedStackPanel stackpanel = new DecoratedStackPanel();
	private VerticalPanel websearch = new VerticalPanel();
	private VerticalPanel imagesearch = new VerticalPanel();
	private VerticalPanel mainpanel = new VerticalPanel();
	private HorizontalPanel toppanel = new HorizontalPanel();
	private TextBox searchtext = new TextBox();
	private AggregatedSearcher aggregatedSearcher;

	private FlickrSearcher flickerSearcher;
	private ArrayList<BaseSearcher> searchers;

	/*
	 * Server Edition: private TomcatServer tomcatServer;
	 */

	public interface Images extends TreeImages {

		AbstractImagePrototype websearch();

		AbstractImagePrototype imagesearch();

	}

	public InfoPane() {

		// Get the images
		Images images = (Images) GWT.create(Images.class);

		stackpanel.add(websearch, getHeaderString("Web Search", images
				.websearch()), true);
		stackpanel.add(imagesearch, getHeaderString("Image Search", images
				.imagesearch()), true);
		
		//searchtext.setSize("200", "25");
		toppanel.add(new HTML("&nbsp;")); 
		toppanel.add(new HTML("&nbsp;"));
		toppanel.add(new HTML("&nbsp;")); 
		toppanel.add(searchtext);
		toppanel.add(new HTML("&nbsp;")); 
		toppanel.add(new HTML("&nbsp;"));
	
		Button querybutton = new Button("Search");
		querybutton.addClickListener(new ClickListener(){

			
			public void onClick(Widget sender) {
				InfoPane.this.InformationRetrivial(new QueryEvent(InfoPane.this, searchtext.getText()));
			}
			
		});
		
		
		toppanel.add(querybutton);
		toppanel.setWidth("100%");
		stackpanel.setWidth("100%");
		
		mainpanel.add(toppanel);
		mainpanel.add(stackpanel);
		
		mainpanel.setWidth("100%");
		initWidget(mainpanel);

		searchers = new ArrayList<BaseSearcher>();

		aggregatedSearcher = new AggregatedSearcher(this);

		flickerSearcher = new FlickrSearcher(this);

	

	}

	/**
	 * Get a string representation of the header that includes an image and some
	 * text.
	 * 
	 * @param text
	 *            the header text
	 * @param image
	 *            the {@link AbstractImagePrototype} to add next to the header
	 * @return the header as a string
	 */
	private String getHeaderString(String text, AbstractImagePrototype image) {
		// Add the image and text to a horizontal panel
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setSpacing(0);
		hPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		hPanel.add(image.createImage());
		HTML headerText = new HTML(text);
		headerText.setStyleName("cw-StackPanelHeader");
		hPanel.add(headerText);

		// Return the HTML string for the panel
		return hPanel.getElement().getString();
	}

	public void addSearchResultView(SearchResultView resultView) {

		websearch.add(resultView);

	}

	public void InformationRetrivial(QueryEvent query) {
		/*
		 * Client Edition:
		 * 
		 * yahooSearcher.doIR(query); googleSearcher.doIR(query);
		 */

		/*
		 * Server Edition: 
		 * yahooSearcher.doIR(q.getQuery());
		 * googleSearcher.doIR(q.getQuery());
		 */

		aggregatedSearcher.doIR(query);
		
	}

	public void addSearcher(BaseSearcher newSE) {

	}

	public void queryEvent(Query query) {
		//Image search
		flickerSearcher.doIR(new QueryEvent(this, query.getQuery()));
		
		fireEvent(new QueryBackEvent(InfoPane.this, query));
	}

	public ArrayList<BaseSearcher> getSearchers() {
		return searchers;
	}

	public void addSearchResultView(SearchResultView resultView, int i) {
		imagesearch.add(resultView);

	}

}
